package br.com.tabletcaixa.saidainterna;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
import br.com.tabletcaixa.db.DBHelper;
import br.com.tabletcaixa.venda.VendaVO;

public class SaidaInternaDAO {

	private SQLiteDatabase db;
	private DBHelper dbHelper;

	private String[] campos = { DBHelper.SAIDA_INTERNA_ID, DBHelper.SAIDA_INTERNA_NUMERO_SAIDA_INTERNA,
			DBHelper.SAIDA_INTERNA_CODIGO, DBHelper.SAIDA_INTERNA_DESCRICAO,
			DBHelper.SAIDA_INTERNA_PRECO, DBHelper.SAIDA_INTERNA_PRECO_TOTAL,
			DBHelper.SAIDA_INTERNA_QUANTIDADE, DBHelper.SAIDA_INTERNA_CLIENTE, 
			DBHelper.SAIDA_INTERNA_VENDEDOR};

	public SaidaInternaDAO(Context context) {

		dbHelper = new DBHelper(context);

	}

	public void conectar() {

		db = dbHelper.getWritableDatabase();

	}

	public void desconectar() {

		dbHelper.close();

	}

	public long cadastrarSaidaInterna(SaidaInternaVO saidaInterna) {

		try {

			ContentValues values = new ContentValues();
			values.put(DBHelper.SAIDA_INTERNA_ID, saidaInterna.getId());
			values.put(DBHelper.SAIDA_INTERNA_NUMERO_SAIDA_INTERNA, saidaInterna.getNumeroSaidaInterna());
			values.put(DBHelper.SAIDA_INTERNA_CODIGO, saidaInterna.getCodigo());
			values.put(DBHelper.SAIDA_INTERNA_DESCRICAO, saidaInterna.getDescricao());
			values.put(DBHelper.SAIDA_INTERNA_PRECO, saidaInterna.getPreco());
			values.put(DBHelper.SAIDA_INTERNA_PRECO_TOTAL, saidaInterna.getPrecoTotal());
			values.put(DBHelper.SAIDA_INTERNA_QUANTIDADE, saidaInterna.getQuantidade());
			values.put(DBHelper.SAIDA_INTERNA_CLIENTE, saidaInterna.getCliente());
			values.put(DBHelper.SAIDA_INTERNA_VENDEDOR, saidaInterna.getVendedor());

			return db.insert(DBHelper.TBL_SAIDA_INTERNA, null, values);

		} catch (Exception e) {

			e.printStackTrace();
			e.getMessage();

		}

		return 0;

	}
	
	public long alterarSaidaInterna(SaidaInternaVO saidaInterna){
		
		try {

			long id = saidaInterna.getId();
			
			ContentValues values = new ContentValues();
			values.put(DBHelper.SAIDA_INTERNA_ID, saidaInterna.getId());
			values.put(DBHelper.SAIDA_INTERNA_NUMERO_SAIDA_INTERNA, saidaInterna.getNumeroSaidaInterna());
			values.put(DBHelper.SAIDA_INTERNA_CODIGO, saidaInterna.getCodigo());
			values.put(DBHelper.SAIDA_INTERNA_DESCRICAO, saidaInterna.getDescricao());
			values.put(DBHelper.SAIDA_INTERNA_PRECO, saidaInterna.getPreco());
			values.put(DBHelper.SAIDA_INTERNA_PRECO_TOTAL, saidaInterna.getPrecoTotal());
			values.put(DBHelper.SAIDA_INTERNA_QUANTIDADE, saidaInterna.getQuantidade());
			values.put(DBHelper.SAIDA_INTERNA_CLIENTE, saidaInterna.getCliente());
			values.put(DBHelper.SAIDA_INTERNA_VENDEDOR, saidaInterna.getVendedor());

			return db.update(DBHelper.TBL_SAIDA_INTERNA, values, DBHelper.SAIDA_INTERNA_ID + " = " + id, null);

		} catch (Exception e) {

			e.printStackTrace();
			e.getMessage();

		}

		return 0;
		
	}

	public long excluirSaidaInterna(SaidaInternaVO saidaInterna) {

		try {

			long id = saidaInterna.getId();

			return db.delete(DBHelper.TBL_SAIDA_INTERNA, DBHelper.SAIDA_INTERNA_ID + " = " + id, null);

		} catch (Exception e) {

			e.printStackTrace();
			e.getMessage();

		}

		return 0;

	}

	public void excluirTodaSaidaInterna(){
		
		db.execSQL("DELETE FROM " + DBHelper.TBL_SAIDA_INTERNA);
		
	}
	
	public List<SaidaInternaVO> consultarSaidaInterna() {

		List<SaidaInternaVO> lstSaidaInterna = new ArrayList<SaidaInternaVO>();
	
		Cursor cursor = db.query(DBHelper.TBL_SAIDA_INTERNA, campos,
				null, null, null, null, null);
		
		cursor.moveToFirst();

		while (!cursor.isAfterLast()) {

			SaidaInternaVO lSaidaInterna = cursorToSaidaInterna(cursor);
			lstSaidaInterna.add(lSaidaInterna);
			cursor.moveToNext();

		}

		cursor.close();
		return lstSaidaInterna;

	}

	private SaidaInternaVO cursorToSaidaInterna(Cursor cursor) {

		SaidaInternaVO saidaInterna = new SaidaInternaVO();
		saidaInterna.setId(cursor.getInt(0));
		saidaInterna.setNumeroSaidaInterna(cursor.getString(1));
		saidaInterna.setCodigo(cursor.getString(2));
		saidaInterna.setDescricao(cursor.getString(3));
		saidaInterna.setPreco(cursor.getDouble(4));
		saidaInterna.setPrecoTotal(cursor.getDouble(5));
		saidaInterna.setQuantidade(cursor.getInt(6));
		saidaInterna.setCliente(cursor.getString(7));
		saidaInterna.setVendedor(cursor.getString(8));

		return saidaInterna;

	}
	
	public long buscarCodigoUltimaSaidaInterna(){
       
		try {
            
        	Cursor cursor = db.query(DBHelper.TBL_SAIDA_INTERNA, new String[]{"MAX(" + DBHelper.SAIDA_INTERNA_ID + ") AS ID"}, 
        			null, null, null, null, null);
        	
			//Cursor cursor = db.rawQuery("SELECT MAX(_id) AS ID FROM venda", null);
			
            if(cursor.moveToLast()){
                return cursor.getLong(0);
            }
            
        } catch (Exception e) {
           
        	e.printStackTrace();
        	
        }
        
        return 0;
    }
	
}
